<template>
<div class="auth-page">
  <div class="container page">
    <div class="row">

      <div class="col-md-6 offset-md-3 col-xs-12">
        <h1 class="text-xs-center">Sign in</h1>
        <p class="text-xs-center">
            <nuxt-link to="/register">Need an account?</nuxt-link>
        </p>

        <ul v-for="(value, key) in errorText" class="error-messages">
          <li>{{key}} {{value}}</li>
        </ul>

        <form @submit.prevent="onSubmit">
          <fieldset  class="form-group">
            <input v-model="user.email" class="form-control form-control-lg" type="text" placeholder="Email">
          </fieldset>
          <fieldset class="form-group">
            <input v-model="user.password" class="form-control form-control-lg" type="password" placeholder="Password">
          </fieldset>
          <button class="btn btn-lg btn-primary pull-xs-right">
            Sign in
          </button>
        </form>
      </div>

    </div>
  </div>
</div>
</template>
<script>
import {login} from '@/api/user'
const Cookie = process.client ? require('js-cookie') : undefined
export default {
    layout: 'index',
    middleware: 'notAuthenticated',
    name: 'LoginIndex',
    data() {
        return {
            user: {
                email: '',
                password: ''
            },
            errorText: {}
        }
    },
    methods: {
        async onSubmit() {
            try {
                //kk123@qq.com
                //dl123456789
                const {data} = await login({user: this.user})
                this.$store.commit('setUser', data.user)
                Cookie.set('user', data.user)
                this.$router.push('/')
            } catch (error) {
                this.errorText = error.response.data.errors
                return false
            }
            
        }
    },
}
</script>
<style>
</style>